<template>
  <div>
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '80px' } }">
        <a-form-item name="dateRange" label="记录时间">
          <a-range-picker v-model:value="formState.dateRange" class="w-64" format="YYYY-MM-DD" value-format="YYYY-MM-DD" :allow-clear="false" />
        </a-form-item>
        <a-form-item name="planName" label="" class="ml-4">
          <a-input v-model:value="formState.planName" class="w-64" placeholder="任务代号/任务名称/后送批号" allow-clear @keydown.enter="getData" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2">
      <div class="flex justify-start">
        <a-button class="mb-2 mr-2" type="primary" @click="onAdd"> 新增 </a-button>
        <BaseExport type="战救" name="后送任务" code="000561" :search-params="formState">导出</BaseExport>
      </div>
      <BaseTable fill :data-source="tableData" row-key="id" :loading="loading" :scroll="{ x: 2400 }" column-code="000561" :pagination="pagination">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'status'">
            <!-- 0：未确认 1：已确认 2：已执行 3:已作废 -->
            <a-tag v-if="record.status === 0" color="orange">未确认</a-tag>
            <a-tag v-if="record.status === 1" color="green">已确认</a-tag>
            <a-tag v-if="record.status === 2" color="blue">已执行</a-tag>
            <a-tag v-if="record.status === 3" color="red">已作废</a-tag>
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-space>
              <a-button type="link" size="small" :disabled="record.status !== 0" @click="onEdit(record)">修改</a-button>
              <BaseButton v-if="record.status === 0" confirm-text="是否确认此任务?" type="link" size="small" @click="onChangeStatus(record, 1)">
                确认
              </BaseButton>
              <BaseButton
                v-if="record.status !== 0"
                :disabled="record.status !== 1"
                confirm-text="是否撤销确认此任务?"
                type="link"
                size="small"
                danger
                @click="onChangeStatus(record, 0)"
              >
                撤销确认
              </BaseButton>
              <a-button type="link" size="small" :disabled="record.status === 2 || record.status === 3" @click="onPersonnel(record)">后送人员</a-button>
              <a-button
                v-if="record.status !== 0 && record.status !== 3"
                :disabled="record.status === 2"
                type="link"
                size="small"
                @click="onChangeStatus(record, 2)"
                >执行完毕</a-button
              >
              <BaseButton v-if="record.status === 0" confirm-text="是否作废此计划单?" type="text" danger size="small" @click="onDelRecord(record)">
                作废
              </BaseButton>
            </a-space>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <AddModal v-model:open="open" :row-data="rowData" @ok="getData" />
    <Personnel v-model:open="personnel" :row-data="rowData" @ok="getData" />
  </div>
</template>

<script setup>
import { ref } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import SearchContainer from '@/components/SearchContainer'
import BaseButton from '@/components/BaseButton'
import { post } from '@/utils/request'
import AddModal from './AddModal.vue'
import Personnel from './Personnel.vue'

const rowData = ref(null)
const formRef = ref(null)
const open = ref(false)
const personnel = ref(false)
const formState = ref({
  dateRange: [dayjs(new Date().setDate(new Date().getDate() - 20)).format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')],
  planName: ''
})
const { loading, tableData, pagination, getData } = useTable({
  url: '/war/warEvacuationMission/getWarEvacuationMissionList',
  immediate: true,
  params: formState.value,
  formatParams: (params) => {
    return {
      startDate: formState.value.dateRange[0],
      endDate: formState.value.dateRange[1],
      planName: params.planName
    }
  }
})

const onEdit = (record) => {
  rowData.value = Object.assign({}, record)
  open.value = true
}
const onAdd = () => {
  rowData.value = null
  open.value = true
}
const onPersonnel = (record) => {
  rowData.value = Object.assign({}, record)
  personnel.value = true
}
const onDelRecord = async (record) => {
  const { code, msg } = await post('/war/warEvacuationMission/doDelWarEvacuationMissionTask', {
    id: record.id
  })
  if (code !== 0) return message.error(msg || '删除失败')
  getData()
  message.success('删除成功')
}
const onChangeStatus = async (record, status) => {
  const { code, msg } = await post('/war/warEvacuationMission/doUpdateWarEvacuationMissionStatus', {
    id: record.id,
    status
  })
  if (code !== 0) return message.error(msg || '操作失败')
  getData()
  message.success('操作成功')
}
</script>
